<?php

class process_details extends a_base_controller_l_checkout {

    private  $form_data;
    private $form_errors;
    
    public function __construct() {
        parent::__construct();        
        $this->get_type_n_fields();        
    }

    private function __handle_form_post() {        
        if ($this->form->valid) {
            $this->srv->save_billing_address($this->get_order_object()->id, (object) $this->form->get_post());
            $this->srv->save_shipping_address($this->get_order_object()->id, (object) $this->form->get_post());
            $this->srv->save_extra_data($this->get_order_object()->id, (object) $this->form->get_post());            
            return TRUE;
        } else {
            return FALSE;
        }
    }
    private function __get_saved_address(){
        $user_service = new user_service_l_user();
        $is_guest = $this->is_guest_order();
        
        $billing  = $this->srv->get_billing_address($this->order_object->id);
        $shipping = $this->srv->get_shiping_address($this->order_object->id);
        $extra    = $this->srv->get_extra_data($this->order_object->id);
        $arr_data = array ();
        
        if($billing){
            foreach($billing->fields as $b){
                $arr_data["billing_".$b] = $billing->$b;
            }
        }else{
            if(!$is_guest){
                $billing = $user_service->get_billing_address(get_logged_user());                
                foreach($billing->fields as $b){
                    $arr_data["billing_".$b] = $billing->$b;
                }
            }
        }
        if($shipping){
             foreach($shipping->fields as $s){
                $arr_data["shipping_".$s] = $shipping->$s;
            }
        }else{
            if(!$is_guest){
                $shipping = $user_service->get_shiping_address(get_logged_user());
                foreach($shipping->fields as $s){
                    $arr_data["shipping_".$s] = $shipping->$s;
                }
            }
        }
        if($extra){
            foreach($extra as $e){
                $arr_data[$e->get_field_details()->field_name] = $e->field_data;
            }
        }

        return $arr_data;
    }
    private function __do_execution($is_post_request){
        event_dispatcher::i()->fire(module_u_checkout_details::EVENT_CHECKOUT_DETAILS,array('status'=>'executing','order_obj'=>$this->order_object));
        if ($is_post_request) {
            if ($this->__handle_form_post()) {
                $this->stage_executed();
                //event_dispatcher::i()->fire(module_u_checkout_details::EVENT_CHECKOUT_DETAILS,array('status'=>'stage_executed'));
                $this->next();
                exit();
            } else {
                //event_dispatcher::i()->fire(module_u_checkout_details::EVENT_CHECKOUT_DETAILS,array('status'=>'has_errors'));
                $this->add_checkout_msg("Please enter required information");
                $this->view("process_details");
            }
        }else{
            //event_dispatcher::i()->fire(module_u_checkout_details::EVENT_CHECKOUT_DETAILS,array('status'=>'view_only'));
            $this->view("process_details");
        }
    }
    public function __handle_executed_before($is_post_request) {
        $this->__do_execution($is_post_request);
    }

    public function __handle_executed_never($is_post_request) {        
        $this->__do_execution($is_post_request);
    }

    public function __handle_invalid_stage(stage_l_checkout $valid_stage) {
        redirect($valid_stage->get_action_url());
    }

    public function stage_id() {
        return "details";
    }

    protected function get_type_n_fields() {
        $saved = $this->__get_saved_address();
        $obj = new details_fields_type_m_order();
        foreach ($obj->get_all_types() as $key => $desc) {

            $field_obj = new details_fields_m_order();
            $fields = $field_obj->where("type", $key)->where('visible', '1')->order_by("order")->get();
            if ($fields->result_count() > 0) {
                $this->form->fieldset($desc);
                foreach ($fields as $field) {
                    $default_value = isset($saved[$field->field_name]) ?  $saved[$field->field_name] : $field->default_value;
                    switch ($field->rendering_class) {
                        case "text":
                            $this->form->text($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                            break;
                        case "textarea":
                            $this->form->textarea($field->field_name, $field->field_description, $field->validation_rule, $default_value, array('title' => $field->field_note));
                            break;
                        case "radiogroup":
                            $radios = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece)
                                $radios[] = explode("=", $piece);
                            $this->form->radiogroup($field->field_name, $radios, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "checkbox":
                            $this->form->br();
                            $this->form->checkbox($field->field_name, $field->values, $field->field_description, $default_value == "checked" ? TRUE : FALSE, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "checkgroup":
                            $options = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece)
                                $options[] = explode("=", $piece);
                            $this->form->checkgroup($field->field_name, $options, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        case "select_one" :
                            $options = array();
                            $pieces = explode(";", $field->values);
                            foreach ($pieces as $piece) {
                                $even_more_pieces = explode("=", $piece);
                                $options[$even_more_pieces[0]] = $even_more_pieces[1];
                            }
                            $this->form->select($field->field_name, $options, $field->field_description, $default_value, $field->validation_rule, array('title' => $field->field_note));
                            break;
                        default:
                            break;
                    }
                    if (strlen($field->field_error_msg) > 0) {
                        $this->form->set_error($field->field_name, $field->field_error_msg);
                    }
                }
            }
        }
        $this->form->indent(150);
        //$this->form->hidden(i_handle_posts::OP_CODE,"create_user");       
        $this->form->submit();
        $this->form_data[] = $this->form->get();
    }

    public function batch_assign() {
        parent::batch_assign();
        $this->assign("form_data", $this->form_data);
        $this->assign("form_errors", $this->form_errors);
    }

}

?>